import jdk.swing.interop.SwingInterOpUtils;

public class test {

    //递归求解汉诺塔问题
    public static int count = 0; //计算河内塔移动次数
    public static void move(char pos1,char pos2){
        count++;
        System.out.print(pos1+"->"+pos2+" ");
    }
    public static void hanoi(int n,char pos1,char pos2,char pos3){
        if(n == 1){
            move(pos1,pos3);
        }else{
            hanoi(n - 1,pos1,pos3,pos2);
            move(pos1,pos3);
            hanoi(n - 1,pos2,pos1,pos3);
        }
    }

    public static void main(String[] args) {
        hanoi(1,'A','B','C');
        System.out.println();
        hanoi(2,'A','B','C');
        System.out.println();
        hanoi(3,'A','B','C');
    }

    //递归求斐波那契数列的第 N 项
    public static int fib(int n){
        if(n == 1 || n == 2){
            return 1;
        }else{
            return fib(n - 1) + fib(n - 2);
        }
    }

    public static void main5(String[] args) {
        System.out.println(fib(5));
    }

    //写一个递归方法，输入一个非负整数，返回组成它的数字之和
    public static int everySum(int n){
        if(n < 10){
            return n;
        }else{
            return n % 10 + everySum(n / 10);
        }
    }

    public static void main4(String[] args) {
        System.out.println(everySum(1234));
    }
    //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4)    （递归）
    public static void print(int n){
        if(n < 10){
            System.out.println(n);
        }else{
            print(n / 10);
            System.out.println(n % 10);
        }
    }

    public static void main3(String[] args) {
        print(1234);
    }

    //递归求 1 + 2 + 3 + ... + 10
    public static int sum(int n){
        if(n == 1){
            return 1;
        }else{
            return n + sum(n - 1);
        }
    }

    public static void main2(String[] args) {
        System.out.println(sum(10));
    }

    //递归求 N 的阶乘
    public static int jc(int n){
        if(n == 1){
            return 1;
        }else{
            return n * jc(n - 1);
        }
    }
    public static void main1(String[] args) {
        System.out.println(jc(5));
    }

}
